<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">修改部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit?'修改部门':'新增子部门'"
        :visible.sync="showDiaLog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="clForm"
      >
        <add-dialog :id="curId" ref="refAddLog" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import AddDialog from './deptDialog.vue'
import { delDepartment, getDepartment } from '@/api/departments'
import { tarnDateToTreeDate } from '@/utils/index'
export default {
  components: {
    AddDialog
  },
  data() {
    return {
      originList: [],
      isEdit: false,
      curId: '',
      showDiaLog: false,
      list: [
        {
          name: '财务部',
          manager: '刘备',
          children: [
            {
              name: '财务核算部',
              manager: '张飞',
              children: []
            },
            {
              name: '税务核算部',
              manager: '关羽',
              children: []
            }
          ]
        }],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    // 页面一加载就调用
    this.getDepartmentsList()
  },
  methods: {
    // 重置表单
    clForm() {
      this.$refs.refAddLog.resetDialog()
    },
    // 查询企业部门列表
    async getDepartmentsList() {
      const res = await getDepartment()
      this.originList = res.data.depts.map(item => {
        return { id: item.id, name: item.name, code: item.code, pid: item.pid }
      })
      res.data.depts.shift()
      // console.log(res)
      this.list = tarnDateToTreeDate(res.data.depts)
    },
    // 新增部门
    async hAdd(id) {
      this.curId = id
      this.showDiaLog = true
      this.isEdit = false
    },
    // 修改部门
    hEdit(id) {
      this.curId = id
      this.showDiaLog = true
      this.isEdit = true
      // 用nextTick解决dialog打开数据相同的问题
      this.$nextTick(() => {
        this.$refs.refAddLog.loadDepartmentById()
      })
    },
    // 删除数据
    hDel(id) {
      this.$confirm('你确定要删除这条数据吗?').then(async() => {
        const res = await delDepartment(id)
        console.log(res)
        this.$message.success(res.message)
        this.getDepartmentsList()
      }).catch(err => console.log(err))
    },
    hSuccess() {
      this.showDiaLog = false
      // this.getDepartmentsList()
    }
  }

}
</script>

<style>

</style>
